<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function __add(){
    global $parent;
    if (empty($parent)){
        $parent=0;
    }
    $select_parent=  select_parent($parent);
    $sel_category=  select_category($category_id);
    echo "
<h1>Add New Thread</h1>        
<form action=\"?mod=thread\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Title</td><td class=\"field_object\">
<input type=\"text\" name=\"title\" class=\"textbox\" size='50'/> </td></tr>
<tr><td class=\"field_name\">Content</td><td class=\"field_object\">
<textarea name=\"content\"></textarea></td></tr>
<tr><td class=\"field_name\">Category Id</td><td class=\"field_object\">
$select_category</td></tr>
<tr><td class=\"field_name\">Created By</td><td class=\"field_object\">
<input type=\"text\" name=\"created_by\" class=\"textbox\"/> </td></tr>
<tr><td class=\"field_name\">Created On</td><td class=\"field_object\">
<input type=\"text\" name=\"created_on\" class=\"textbox\" size='20'/> </td></tr>
<tr><td class=\"field_name\">Parent Thread</td><td class=\"field_object\">
$select_parent</td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>";
}
function __edit(){
    global $db,$id;
    if (!empty($id)){
        $sql="select * from q_threads where _id=$id";
        $row=$db->fetch_row($sql);
        extract($row);
        $sel_category=  select_category($category_id);
        $select_parent=  select_parent($parent_thread);
        echo "
<h1>Update Thread</h1>        
<form action=\"?mod=thread\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
    <input type=\"hidden\" name=\"_id\" value=\"$id\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\"> Id</td><td class=\"field_object\">
$id </td></tr>
<tr><td class=\"field_name\">Title</td><td class=\"field_object\">
<input type=\"text\" name=\"title\" class=\"textbox\" value=\"$title\" size='50'/> </td></tr>
<tr><td class=\"field_name\">Content</td><td class=\"field_object\">
<textarea name=\"content\" class=\"textbox\" rows='6' cols='40'>$content</textarea> </td></tr>
<tr><td class=\"field_name\">Category Id</td><td class=\"field_object\">
$sel_category </td></tr>
<tr><td class=\"field_name\">Created By</td><td class=\"field_object\">
<input type=\"text\" name=\"created_by\" class=\"textbox\" value=\"$created_by\"/> </td></tr>
<tr><td class=\"field_name\">Created On</td><td class=\"field_object\">
<input type=\"text\" name=\"created_on\" class=\"textbox\" size='30'  value=\"$created_on\"/> </td></tr>
<tr><td class=\"field_name\">Parent Thread</td><td class=\"field_object\">
$select_parent </td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>";
    }
    
}
function add_process(){
    global $db,$_id,$title,$content,$category_id,$created_by,$created_on,$parent_thread;
    $sql="insert into q_threads values (null,'$title','$content','$category_id',
        '$created_by',sysdate(),$parent_thread,)";
    $db->query($sql);
    refresh_to("?mod=thread", "Back to Thread List", 2);
}   
function getmaxPage(){
    global $db,$pagesize;
    $sql="select count(*) from q_threads where parent_thread=0";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        $rcount=$row[0];
          
        return $rcount/$pagesize;
    } else return 1;
}
function edit_process(){
    global $db,$_id,$title,$mod,$file,$content,$category_id,$created_by,$created_on,$parent_thread;
    $sql="update q_threads set title='$title',content='$content',
        category_id='$category_id',created_by='$created_by',created_on='$created_on',parent_thread='$parent_thread' where _id=$_id";
    $db->query($sql);
    refresh_to("?mod=thread", "Back to Thread List", 2);
}
function cekJawaban($thread_id){
    global $db;
    $sql="select _id from q_threads where parent_thread=$thread_id";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        return $row[0];
    } else return 0;
}
function _default(){
    global $db,$page,$pagesize,$mod,$file;
    if (empty($page)){
        $start=0;
     } else {
        $start=$page * $pagesize; 
    }
    echo "<h2>Thread List</h2>";
    echo "<a href='?mod=thread&action=add'>Add New Thread</a>";
    echo "<table width='100%' cellpadding='4' cellspacing='0'>
        <tr><th> Id</th><th width='250'>Title</th><th>Category</th>
        <th>Created By</th><th>Created On</th><th>Action</th></tr>";
    $sql="select a.*,b.category from q_threads a,q_categories b 
        where a.parent_thread=0 and a.category_id=b._id
        order by _id limit $start,$pagesize";
    $result=$db->query($sql);
    while($row=$db->fetch_array($result)){
        extract($row);
        echo "<tr><td>$_id</td><td>$title</td><td>$category</td>
            <td>$created_by</td><td>$created_on</td>
                <td align='center'><a href=\"?mod=thread&action=edit&id=$_id\">
        <img  src='img/icons/edit.gif' title='Edit'/></a>
        <a href=\"?mod=thread&action=delete&id=$_id\"><img  
            src='img/icons/delete.gif' title='Delete'/></a>";
        $jawaban=  cekJawaban($_id);
        if ($jawaban==0){
            echo "<a href='?mod=thread&action=add&parent=$_id'>
                <img  src='img/icons/add_answ.gif' title='Add Answer'/></a>";
        } else {
            echo "<a href='?mod=thread&action=edit&id=$jawaban'>
                <img  src='img/icons/edit_answ.gif' title='Edit Answer'/></a>";
        }
        echo "</td></tr>";
    }
    
    echo "<tr><td colspan='6'> Page : ";
    $pagecount=  getmaxPage();
    for ($i=0;$i<$pagecount;$i++){
        $vpage=$i+1;
        echo "<a href='?mod=thread&page=".$i."'>$vpage</a> | ";
    }
    echo "</td></tr></table>";
}
global $action;
switch($action){
    case 'add':
        __add();
        break;
    case 'add_process':
        add_process();
        break;
    case 'edit':
        __edit();
        break;
    case 'edit_process':
        edit_process();
        break;
    default:
        _default();
}
?>
